package com.jk.tweetsnear.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;
import com.jk.tweetsnear.R;
import com.jk.tweetsnear.application.App;
import com.jk.tweetsnear.utils.Utils;

public class TabMenuFragment extends Fragment
{
	private Button btnTabList;
	private Button btnTabMap;
	private Button btnTabTwitter;
	
	private Fragment newFragment;
	private Tracker mGaTracker;
	private GoogleAnalytics mGaInstance;		
	
	public static final int TAB_POSITION_LIST = 0;
	public static final int TAB_POSITION_MAP = 1;
	
	@Override
	public void onStart()
	{
		super.onStart();
		mGaTracker.sendView("/TabMenuFragment");
	}		
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.tab_menu_fragment, null);
		
		mGaInstance = GoogleAnalytics.getInstance(App.act);
		mGaTracker = mGaInstance.getTracker(Utils.TRACKER_ID); 			
		
		setTabMenu(view);
		moveFragment(TAB_POSITION_LIST);
		
		return view;
	}
	
	private void moveFragment(int position)
	{
		FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
		
		
		switch (position)
		{
		case TAB_POSITION_LIST:
			btnTabList.setBackgroundResource(R.color.kakaotalk_blue);
			btnTabMap.setBackgroundResource(R.color.Ivory);			
			
			if(newFragment instanceof TweetListFragment)
				return;
			
			newFragment = new TweetListFragment();	
			break;
		case TAB_POSITION_MAP:
			btnTabList.setBackgroundResource(R.color.Ivory);
			btnTabMap.setBackgroundResource(R.color.kakaotalk_blue);					
			
			if(newFragment instanceof TweetMapFragment )
				return;
			
			newFragment = new TweetMapFragment();	
			break;
		}

		fragmentTransaction.replace(R.id.fragment_contents, newFragment);
		fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		fragmentTransaction.commit();		
	}
	
	private void setTabMenu(View view)
	{
		btnTabList = (Button) view.findViewById(R.id.tab_list);
		btnTabMap = (Button) view.findViewById(R.id.tab_map);
		btnTabTwitter = (Button) view.findViewById(R.id.tab_twitter);
		
		btnTabList.setOnTouchListener(onTouch);
		btnTabMap.setOnTouchListener(onTouch);
		btnTabTwitter.setOnTouchListener(onTouch);
	}
	
	OnTouchListener onTouch = new OnTouchListener()
	{
		
		@Override
		public boolean onTouch(View v, MotionEvent event)
		{
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				switch (v.getId())
				{
				case R.id.tab_list:
					btnTabList.setBackgroundResource(R.color.kakaotalk_blue);
					break;
				case R.id.tab_map:
					btnTabMap.setBackgroundResource(R.color.kakaotalk_blue);	
					break;
				case R.id.tab_twitter:
					break;
				}
				
				return true;
			}

			if (event.getAction() == MotionEvent.ACTION_UP)
			{

				switch (v.getId())
				{
				case R.id.tab_list:
					mGaTracker.sendEvent("ui_action", "tab_press", "tab_list", 0L);							
					
					btnTabList.setBackgroundResource(R.color.Ivory);
					moveFragment(TAB_POSITION_LIST);		
					break;
				case R.id.tab_map:
					mGaTracker.sendEvent("ui_action", "tab_press", "tab_map", 0L);	
					
					btnTabTwitter.setBackgroundResource(R.color.Ivory);	
					moveFragment(TAB_POSITION_MAP);
					break;
				case R.id.tab_twitter:
					mGaTracker.sendEvent("ui_action", "tab_press", "tab_twitter", 0L);	
					
					try
					{
						Intent intent = App.act.getPackageManager().getLaunchIntentForPackage("com.twitter.android");
						if(intent == null)
						{
							Intent marketLaunch = new Intent(Intent.ACTION_VIEW); 
							marketLaunch.setData(Uri.parse("market://details?id=com.twitter.android")); 
							startActivity(marketLaunch);
							break;
						}
						startActivity(intent);
					} catch (Exception e)
					{
						Intent marketLaunch = new Intent(Intent.ACTION_VIEW); 
						marketLaunch.setData(Uri.parse("market://details?id=com.twitter.android")); 
						startActivity(marketLaunch);
					}
					break;					
				}
				return true;
			}		

			return false;
		}
	};
}
